﻿using DAO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Thefaceless
{
    public partial class FBill : Form
    {
        public FBill()
        {
            InitializeComponent();
        }

        public void ClearTextbox()
        {
            this.txtDay.Clear();
            this.txtMaHD.Clear();
            this.txtTenNV.Clear();
            this.txtTotal.Clear();
            this.txtValue.Clear();
        }

        private void LoadBill()
        {
            DataProvider dp = new DataProvider();
            String sql = "select * from HOADON";
            DataTable db = dp.ExecuteQuery(sql);
            this.dataBill.DataSource = db;
        }

        private void FBill_Load(object sender, EventArgs e)
        {
            LoadBill();
        }

        // Load chi tiet hoa don theo hang  dang click
        private void LoadBillDetail(int index)
        {
            DataProvider dp = new DataProvider();
            String sql = "select * from CHITIETHOADON where MaHD = '" + this.txtMaHD.Text.ToString() + "'";
            DataTable db = dp.ExecuteQuery(sql);
            this.dataDetail.DataSource = db;
        }

        // Load Textfield hoa don hang dang click
        private void LoadTextBox(int index)
        {
            DataGridViewRow row = this.dataBill.Rows[index];
            this.txtMaHD.Text = row.Cells[0].Value.ToString();
            this.txtTenNV.Text = row.Cells[1].Value.ToString();
            this.txtDay.Text = row.Cells[2].Value.ToString();
            this.txtTotal.Text = row.Cells[3].Value.ToString();
        }

        // Bat du lieu hoa don hang dang click
        private void dataBill_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            if (index >= 0 && index < this.dataBill.RowCount)
            {
                LoadTextBox(index);
                LoadBillDetail(index);
            }
        }

        // Kiem tra gia tri cot tim kiem
        private String checkSearchValue()
        {
            String value = this.cbKeyFind.Text;
            if (value.Equals("Mã hóa đơn"))
                return "MaHD";
            else if (value.Equals("Mã nhân viên"))
                return "MaNV";
            else if (value.Equals("Ngày hóa đơn(mm-dd-yyyy)"))
                return "NgayHD";
            return null;
        }

        // Bat su kien nut tim kiem
        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (checkSearchValue() != null)
            {
                string value = txtValue.Text;
                String sql;
                if (!value.Equals(""))
                    sql = "select * from HOADON WHERE " + checkSearchValue() + "='" + value + "'";
                else
                    sql = "select * from HOADON";
                DataProvider dp = new DataProvider();
                DataTable dt = dp.ExecuteQuery(sql);
                this.dataBill.DataSource = dt;

            }
        }

        // Bat su kien nut Huy bo
        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearTextbox();
            LoadBill();
            this.cbKeyFind.Text = "";
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }
    }
}
